<?php


class Md5Behavior extends CActiveRecordBehavior{
	
	public $md5Attribute;
	
	public function beforeSave($event){
		$owner = $this->getOwner();
		if($this->IsMd5Pwd($owner->{$this->md5Attribute}) == false){
			$owner->{$this->md5Attribute} = md5($owner->{$this->md5Attribute});
		}
	}
	
	public function IsMd5Pwd($string){
		return strlen($string) == 32;
	}
	
	public function afterFind($event){
		$owner = $this->getOwner();
		if($this->IsMd5Pwd($owner->{$this->md5Attribute}) == false){
			$owner->{$this->md5Attribute} = md5($owner->{$this->md5Attribute});
			$owner->update(array($this->md5Attribute));
		}
	}
	
}